package com.beiming.kotlindemo

import java.util.*
import kotlin.math.max

/**
 * Created by lingchen on 2020/4/27. 22:08
 * mail:lingchen52@foxmail.com
 */
class LearnKotlin {

    ///////////////////////////////////////////////////////////////////
    //函数（方法）
    //////////////////////////////////////////////////////////////////
    fun main() {
        val a = 10
        println("a = $a")

        var b = 10
        b = b * 10
        println("b = $b")
    }

    fun methodName(param1: Int, param2: Int): Int {
        val c = 0
        return c
    }

    fun largerNamber(num1: Int, num2: Int): Int {
        return max(num1, num2)
    }

    //方法内只有一行代码的方法的语法糖
    fun largerNumber2(num1: Int, num2: Int): Int = max(num1, num2)

    //类型推导可省略返回值类型定义
    fun largerNumber3(num1: Int, num2: Int) = max(num1, num2)

    //////////////////////////////////////////////////////////////////
    //if条件语句
    //////////////////////////////////////////////////////////////////
    fun largerNumber4(num1: Int, num2: Int): Int {
        var value = 0
        if (num1 > num2) {
            value = num1
        } else {
            value = num2
        }
        return value
    }

    //有返回值的if语句
    fun largerNumber5(num1: Int, num2: Int): Int {
        val value = if (num1 > num2) {
            num1
        } else {
            num2
        }
        return value
    }

    //简化
    fun largerNumber6(num1: Int, num2: Int): Int {
        return if (num1 > num2) {
            num1
        } else {
            num2
        }
    }

    //进一步简化
    fun largerNumber7(num1: Int, num2: Int) = if (num1 > num2) {
        num1
    } else {
        num2
    }

    //最简
    fun largerNumber8(num1: Int, num2: Int) = if (num1 > num2) num1 else num2

    //////////////////////////////////////////////////////////////////
    //when条件语句
    //////////////////////////////////////////////////////////////////

    fun getScore(name: String) = if (name == "Tom") {
        86
    } else if (name == "Jim") {
        77
    } else if (name == "Jack") {
        95
    } else if (name == "Lily") {
        100
    } else {
        0
    }

    //简化
    fun getScore1(name: String) = when (name) {
        "Tom" -> {
            86
        }
        "Jim" -> {
            77
        }
        "Jack" -> {
            95
        }
        "Lily" -> {
            100
        }
        else -> {
            0
        }
    }

    //只有一行代码时，可以省略{}
    fun getScore2(name: String) = when (name) {
        "Tom" -> 86
        "Jim" -> 77
        "Jack" -> 95
        "Lily" -> 100
        else -> 0
    }

    //is关键字用于类型匹配
    fun checkNumber(num: Number) {
        when (num) {
            is Int -> println("number is Int")
            is Double -> println("number is Double")
            else -> println("number not support")
        }
    }

    //when不带参数的用法
    fun getScore3(name: String) = when {
        name == "Tom" -> 86
        name == "Jim" -> 77
        name == "Jack" -> 95
        name == "Lily" -> 100
        else -> 0
    }

    fun getScore4(name: String) = when {
        name.startsWith("Tom") -> 86
        name.startsWith("Jim") -> 77
        name.startsWith("Jack") -> 95
        name.startsWith("Lily") -> 100
        else -> 0
    }

    //////////////////////////////////////////////////////////////////
    //循环语句
    //////////////////////////////////////////////////////////////////

    //区间 闭区间[0,10]
    var range = 0..10

    fun testRange() {
        for (i in 0..10) {
            println(i)
        }
    }

    //左闭右开区间 [0,10)
    var range1 = 0 until 10

    //递增step
    fun testRange1(){
        for (i in 0 until 10 step 2){
            println(i)
        }
    }

    //降序区间 [10,1]
    fun testRange2(){
        for (i in 10 downTo 1 step 2){
            println(i)
        }
    }


}